<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Simple GIGen editor</title>
    <link href="CSS/styles.css" type="text/css" rel="Stylesheet" />
    <script language="javascript" type="text/javascript" src="../../../Script/GrammarIntelliForJS.js"></script>
    <script language="javascript" type="text/javascript" src="../../../Script/GrammarIntelliForJsUIHelper.js"></script>
    <script language="javascript" type="text/javascript">
        var oGIMachine = new GIMachine("SimpleCustomerInfoQuery"), oCustomerCheckState = null;
        
        // Create the last state first, so that we can "attach" it while forming the literal ones
        oCustomerCheckState = new GIMachineWordState("^[a-zA-Z]+$" /* Pattern */,
                                                     false /* This is not a literal text/pattern */,
                                                     3 /*can be any number for tracking*/,
                                                     "CustomerName" /*This name will can be used to capture result*/,
                                                     true /* final state of the query */);
        oGIMachine.States[0] = GetLiteralPattern("show info for", 0, oCustomerCheckState);

        function ShowMachineInfo() {
            // Its good to see what your GIMachine looks like:
            document.getElementById("txtOutput").value = oGIMachine.ToXML();  // Uncomment if you want to see XML
        }

        function ShowIntellisense() {
            var sText = document.getElementById("txtUserInput").value, oDiv = document.getElementById("divSuggestions"), sHTML = "", i = 0, oTrace = document.getElementById("txtOutput"), arrSuggestions = null;
            //arrSuggestions = oGIMachine.Suggestions(sText, oTrace); // With trace ... good for debugging
            arrSuggestions = oGIMachine.Suggestions(sText); // no tracing

            if (arrSuggestions != null && arrSuggestions.length > 0) {
                //oTrace.value += arrSuggestions.length + " suggestions for '" + sText + "'";
                for (i = 0; i < arrSuggestions.length; i++) {
                    if (arrSuggestions[i].IsLiteral) {
                        sHTML += "<b>L:</b> &nbsp;" + arrSuggestions[i].Pattern + "<br>";
                    } else {
                        sHTML += "<b>R:</b> &nbsp;" + arrSuggestions[i].Pattern + "  [" + arrSuggestions[i].Name + "]<br>";
                    }
                }
                oDiv.innerHTML = sHTML;
            } else {
                oTrace.value = "0 suggestions for '" + sText + "'";
                oDiv.innerHTML = "";
            }
        }

        function ParseText() {
            var sText = document.getElementById("txtUserInput").value, oDiv = document.getElementById("divSuggestions"), oTrace = document.getElementById("txtOutput"), oMatchCollection = null;
            //oMatchCollection = oGIMachine.ProcessText(sText, oTrace);
            oMatchCollection = oGIMachine.ProcessText(sText);

            if (oMatchCollection != null) {
                var sCustomerName = "";
                oTrace.value = oMatchCollection.ToXML();

                // Now, its time to get the result ... lets capture the "CustomerName" that user entered
                sCustomerName = oMatchCollection.NamedMatches["CustomerName"].Value;
                // At this point you can do whatever you want with the data ... we'r just displaying it off.
                oDiv.innerHTML = "Result: Captured customerName = '<b>" + sCustomerName + "'</b>";
            } else {
                oTrace.value = "No matches...";
            }
        }
        
    </script>
</head>
<body onload="ShowMachineInfo();">
    <div class="Header">Simple GIGen Editor</div>
    <hr color="#cecece" />
    <input type="text" id="txtUserInput" size="100" class="UserInput"  />&nbsp;<input type="button" value="Add" class="btnAdd" onclick="AddPattern()" /> <br />
    <p class="SubHeader">Patterns Added</p>    
    <table class="PatternTable">
        <tr>
            <th>Pattern Name</th>
            <th>Expression</th>
            <th>&nbsp;</th>
            <th>&nbsp;</th>
            <th>&nbsp;</th>
        </tr>
    </table>
</body>
</html>
